package com.shk.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.shk.dao.OrderSettingDao;
import com.shk.pojo.OrderSetting;
import com.shk.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> list) {

        if(list !=null && list.size()>0){
           for(OrderSetting orderSetting :list){
             Date orderDate= orderSetting.getOrderDate();
               System.out.println(orderSetting.getOrderDate());
               SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
               String format = sdf.format(orderDate);
               long count= orderSettingDao.findCountByDate(format);

               try {
                   Date date = sdf.parse(format);
               } catch (ParseException e) {
                   e.printStackTrace();
               }
               if(count>0){

                    orderSettingDao.updateByDate(orderSetting);
              }else {
                  orderSettingDao.add(orderSetting);
              }
           }
        }
    }

    @Override
    public List<Map> show(String date) {
        String begin = date+"-1";
        String end = date+"-31";

        Map map = new HashMap();
       map.put("begin",begin);
        map.put("end",end);

        List<Map> list = orderSettingDao.show(map);
        /*处理成前端想要的数据类型
        * date: 1, number: 120, reservations: 1*/
       /* ArrayList<Map> objects = new ArrayList<>();
        if(list != null && list.size()>0){
           for(OrderSetting orderSetting : list){
               Map hashMap = new HashMap();
               hashMap.put("date",orderSetting.getOrderDate().getDate());
               hashMap.put("number",orderSetting.getNumber());
               hashMap.put("reservations",orderSetting.getReservations());
               objects.add(hashMap);
           }
        }*/

        return list;
    }

    @Override
    public void setNumber(OrderSetting orderSetting) {
        orderSettingDao.setNumber(orderSetting);
    }

    //实战新增，定时清理预约设置
    public void clearOrdersetting() {
        orderSettingDao.clearOrdersetting(new Date());
    }
}
